﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Principal;
using XCommunity.Documents;
using XCommunity.Frames;
using XCommunity.Caching;
using XCommunity.Members;
using XCommunity.Forums;
using XCommunity.Blogs;

namespace XCommunity.Web
{
  public sealed class WebCommunityContext : ICommunityContext
  {

    private const string InstanceKey = "XCommunity.WebCommunity by Ivony";


    private static WebCommunityContext _instance;

    public static WebCommunityContext Current
    {
      get
      {
        if ( _instance == null )
          _instance = new WebCommunityContext();

        return _instance;
      }
    }


    public static ICommunityContext CurrentCommunityContext
    {
      get { return Current; }
    }


    public static ICommunityContext GetCurrentContext()
    {
      return Current;
    }


    private WebCommunityContext()
    {
      Inited = false;
    }

    public void Init()
    {

      _cacheService = new CacheService( this );
      _documentService = new DocumentService( this );
      _frameService = new FrameService( this );

      _accountService = new WebAccountService( this );

      _memberService = new MemberService( _accountService, this );


      _forumsSystem = new ForumsSystem( this );

      _blogSystem = new BlogSystem( this );


      
      _cacheService.Init();
      _documentService.Init();
      _frameService.Init();

      _accountService.Init();

      _memberService.Init();


      _forumsSystem.Init();

      
      Inited = true;
    }


    public bool Inited
    {
      get;
      private set;
    }



    private WebAccountService _accountService;

    public WebAccountService AccountService
    {
      get { return _accountService; }
    }



    IIdentity ICommunityContext.CurrentAccount
    {
      get { return AccountContext.CurrentAccount; }
    }


    private MemberService _memberService;

    MemberService ICommunityContext.MemberService
    {
      get { return _memberService; }
    }

    private CacheService _cacheService;

    CacheService ICommunityContext.CacheService
    {
      get { return _cacheService; }
    }

    private DocumentService _documentService;

    DocumentService ICommunityContext.DocumentService
    {
      get { return _documentService; }
    }

    private FrameService _frameService;

    FrameService ICommunityContext.FrameService
    {
      get { return _frameService; }
    }

    ICommunitySystem ICommunityContext.GetCommunitySystem( Guid communitySystemId )
    {
      return null;
    }





    private ForumsSystem _forumsSystem;

    public ForumsSystem ForumsSystem
    {
      get { return _forumsSystem; }
    }


    private BlogSystem _blogSystem;

    public BlogSystem BlogSystem
    {
      get { return _blogSystem; }
    }



    public IHtmlDocumentProvider GetDefaultDocumentProvider()
    {
      return new UbbTextDocumentPlugin();
    }

  }
}
